package code.Array;

import java.util.Arrays;
import java.util.logging.Logger;

/**
 * Given integer array nums, return the third maximum number in this array. If the third maximum does not exist, return the maximum number.
 * <p>
 *  
 * <p>
 * Example 1:
 * <p>
 * Input: nums = [3,2,1]
 * Output: 1
 * Explanation: The third maximum is 1.
 * Example 2:
 * <p>
 * Input: nums = [1,2]
 * Output: 2
 * Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
 * Example 3:
 * <p>
 * Input: nums = [2,2,3,1]
 * Output: 1
 * Explanation: Note that the third maximum here means the third maximum distinct number.
 * Both numbers with value 2 are both considered as second maximum.
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/third-maximum-number
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class thirdMax {
    public static void main(String[] args) {
        int arr[] = {2, 2, 3, 1};
        System.out.println(solveSolution_2(arr));
    }

    private static int solveSolution(int[] nums) {
        //废弃 理由：案例二中 类似于这样的如果重复太多，就退化为冒泡 而且有多少个相等不能提前得知
        System.out.println("Input:" + Arrays.toString(nums));
        if (nums.length < 3) {
            if (nums.length == 1) {
                return nums[0];
            } else {
                if (nums[0] < nums[1]) {
                    return nums[1];
                } else {
                    return nums[0];
                }
            }

        } else {
            for (int i = 0; i < 4; i++) {
                //冒泡排序
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[i] > nums[j]) {
                        int temp = nums[j];
                        nums[j] = nums[i];
                        nums[i] = temp;
                    }
                }
            }
            return nums[nums.length - 3];
        }
    }

    private static int solveSolution_2(int[] nums) {
        System.out.println("Input:" + Arrays.toString(nums));
        long firstMax = Long.MIN_VALUE; //负的二的63次方
        long secondMax = Long.MIN_VALUE; //负的二的63次方
        long thirdMax = Long.MIN_VALUE; //负的二的63次方
        //int secondMax = Integer.MIN_VALUE; //负的二的31次方
        //System.out.println("firstMax:" + firstMax);
        //System.out.println("firstMax:" + Integer.MIN_VALUE);
        if (nums.length < 3) {
            if (nums.length == 1) {
                return nums[0];
            } else {
                return Math.max(nums[0], nums[1]);
            }

        } else {
            for (int num : nums) {
                if (num == firstMax || num == secondMax || num == thirdMax) continue;
                if (num > firstMax) {
                    thirdMax = secondMax;
                    secondMax = firstMax;
                    firstMax = num;
                } else if (num > secondMax) {
                    thirdMax = secondMax;
                    secondMax = num;
                } else if (num >= thirdMax) {
                    thirdMax = num;
                }
            }

            if (thirdMax == Long.MIN_VALUE) {
                return (int) firstMax;//第三大的数不存在, 所以返回最大的数。
            } else {
                return (int) thirdMax;
            }
        }

    }
}
